
# Chapter2: four build-in data structures: lists, dictionaries, tuples, and sets

# for + lists p60
vowels = ['a', 'e', 'i', 'o', 'u']
word = "Milliways"
for letter in word:
 if letter in vowels:
     print(letter)

#version 2
vowels = ['a', 'e', 'i', 'o', 'u']
word = "Milliways"
found = []  #Start with an empty list
for letter in word:
 if letter in vowels:
    if letter not in found:
       found.append(letter)
for vowel in found:
 print(vowel)

# version3 更具一般性和交互性
vowels = ['a','e','i','o','u']
word = input("Provide a word to search for vowels:")
found = []
for letter in word:
     if letter in vowels:
        if letter not in found:
              found.append(letter)
for vowel in found:
     print(vowel)

# 4 methods: remove, pop, extend, and  insert
names = ["Mike","Mary","Jan","Jack"]


print(names)
names.insert(1,"Dan") #list增：insert指定位置增加数据
print(names)
names[3] = "Zhichao" #list改：直接找到列表中某个值，然后赋新值
print(names)
names.remove("Zhichao") #list删：remove指定某个数据进行删除
print(names)
del names[1] #list删： del指定[x]的某个数据位置进行删除
print(names)
names.pop() #list删：Remove and return item at index (default last) 不指定默认删除最后一个值
print(names)
names.pop(1) #lsit删：如果加上指定位置，names.pop(1)<==> del names[1]
print(names)

# (What Looks Like a Copy, But Isn’t)
# first = [1,2,3,4,5]
# second = first
# print(second)
# second.append(6)  # bug
# print(first)

# 正确复制列表方式
first = [1,2,3,4,5]
second = first
print(second)
second.append(6)
print(first)
third = second.copy()
third.append(7)
print(second,third)


# p67 "Don't panic!" => list => "on tap"
phrase = "Don't panic!"
plist = list(phrase)  # str 转换 list 使用的方法是 list(str)
print(phrase)    # Each time the for loop iterates, plist shrinks by one object until the last four objects are gone
print(plist)

for i in range(4): #This small loop pops the last four objects from “plist”. No more “nic!”.
    # 循环执行4次 删除末位 使用方法 pop()
    plist.pop()  #Get rid of the ‘D’ at the start of the list
plist.pop(0)   # With the letter D popped off the front of the list, a call to remove dispatches with the apostrophe
plist.remove("'")      # remove, the apostrophe from the list
# extend ==> plist + ["a","p"]
plist.extend([plist.pop(),plist.pop()]) # 运算顺序，（1*（5+1））
#['o','n','t',' ']   a           p

plist.insert(2,plist.pop(3))
#                   ' '
new_phrase = ''.join(plist) #? ''.join() list 转换 str
print("plist=",plist) # list的方法会打乱和改变list的内容
print("new_phrase=",new_phrase)

#  letters
# ['D', 'o', 'n', "'", 't', ' ', 'p', 'a', 'n', 'i', 'c', '!']

#  letters[0:10:3]
# ['D', "'", 'p', 'i']      Every third letter up to (but not including) index location 10

#  letters[3:]
# ["'", 't', ' ', 'p', 'a', 'n', 'i', 'c', '!']  Skip the first three letters, then give me everything else

#  letters[:10]
# ['D', 'o', 'n', "'", 't', ' ', 'p', 'a', 'n', 'i'] All letters up to (but not including) index  location 10

#  letters[::2]
# ['D', 'n', 't', 'p', 'n', 'c'] Every second letter

# p82 panic2
phrase = "Don't panic!"
plist = list(phrase)
print(phrase)
print(plist)
print(plist)
print(new_phrase)
new_phrase = ''.join(plist[1:3])   # started by slicing out the word “on” from “plist”.
new_phrase = new_phrase + ''.join([plist[5], plist[4], plist[7], plist[6]])  #then picked out each additional letter that we needed: space, “t”, “a”, and “p

#  for loop
paranoid_android = "Marvin"
letters = list(paranoid_android)
for char in letters :
    print('\t',char)

# slices
paranoid_android = "Marvin, the Paranoid Android"
letters = list(paranoid_android)
for char in letters[:6]:
    print('\t', char)
print()
for char in letters[-7:]:
    print('\t'*2,char)  #  multiplication operator (*)
print()
for char in letters[12:20]:
     print('\t'*3,char)

